package com.tdg.qywx.controller;

import com.tdg.qywx.core.TDGResponse;
import com.tdg.qywx.core.enums.TDGResponseCode;
import com.tdg.qywx.service.PerService;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/tdg/qywx/api2/v1/operate")
public class OperateController {
    private static final Logger _logger = LoggerFactory.getLogger(OperateController.class);

    @Autowired
    private PerService perService;

    @Operation(summary = "获取部门所有在线员工")
    @GetMapping("/searchPersonAllByBM")
    public TDGResponse searchPersonAllByBM(String bmid){
        if (StringUtils.isBlank(bmid)) {
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"部门ID不可为空");
        }
        _logger.debug("==> searchPersonAllByBM ==> Paramers: bmid:{}", bmid);
        return perService.searchPersonAllByBM(bmid);
    }

    @Operation(summary = "获取所有在线员工")
    @GetMapping("/searchPersonAll")
    public TDGResponse searchPersonAll(){
        _logger.debug("==> searchPersonAll ==> Paramers: none");
        return  perService.searchPersonAllByBM("");
    }

    @Operation(summary = "获取所有在线员工的部门")
    @GetMapping("/searchBM")
    public TDGResponse searchBM(){
        _logger.debug("==> searchBM ==> Paramers: none");
        return perService.searchBM();
    }

    @Operation(summary = "查询工单基本信息")
    @GetMapping("/searchGD")
    public TDGResponse searchGD(String gdh){
        if (StringUtils.isBlank(gdh)) {
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }
        _logger.debug("==> searchGD ==> Paramers:gdh:{}", gdh);
        return  perService.searchGD(gdh);
    }

    @Operation(summary = "模糊查询工单号")
    @GetMapping("/searchGDLike")
    public TDGResponse searchGDLike(String gdh){
        if (StringUtils.isBlank(gdh)) {
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }
        _logger.debug("==> searchGDLike ==> Paramers:gdh:{}", gdh);
        return perService.searchGDLike(gdh);
    }

    @Operation(summary = "查找所有工单")
    @GetMapping("/findMoByAll")
    public TDGResponse findMoByAll(){
        _logger.debug("==> findMoByAll ==> Paramers:none");
        return perService.findMoByAll();
    }

    @Operation(summary = "查询工单是否存在")
    @GetMapping("/searchGYD")
    public TDGResponse searchGYD(String gdh){
        if (StringUtils.isBlank(gdh)) {
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }

        _logger.debug("==> searchGYD ==> Paramers: gdh:{}", gdh);
        return perService.searchGYD(gdh);
    }

    @Operation(summary = "查询员工基本信息")
    @GetMapping("/searchPerson")
    public TDGResponse searchPerson(String id, @RequestParam(required = false) String mo, @RequestParam(required = false) String sec){
        if (StringUtils.isBlank(id)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工号不可为空");
        }
        _logger.debug("==> searchPerson ==> Paramers:id:{}, mo:{}, sec:{}", id,mo,sec);
        return perService.searchPerson(id, mo, sec);
    }

    @Operation(summary = "查询当前工单和工艺段的上线员工")
    @GetMapping("/searchGDSetPer")
    public TDGResponse searchGDSetPer(String gdh,String sec){
        if (StringUtils.isBlank(gdh)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }
        if (StringUtils.isBlank(sec)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工艺段不可为空");
        }

        _logger.debug("==> searchGDSetPer ==> Paramers: gdh:{}, sec:{}", gdh,sec);
        return perService.searchGDSetPer(gdh, sec);
    }

    @Operation(summary = "员工确认上线")
    @GetMapping("/confirmUp")
    public TDGResponse confirmUp(String functionID,String opid, String id,String username,String bb,String mo,String scxm,String sec){

        if (StringUtils.isBlank(functionID)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作模块不可为空");
        }
        if (StringUtils.isBlank(opid)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作号不可为空");
        }
        if (StringUtils.isBlank(id)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工号不可为空");
        }
        if (StringUtils.isBlank(username)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"姓名不可为空");
        }
        if (StringUtils.isBlank(bb)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"班别不可为空");
        }
        if (StringUtils.isBlank(mo)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }
        if (StringUtils.isBlank(scxm)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"生产线码不可为空");
        }
        if (StringUtils.isBlank(sec)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工艺段不可为空");
        }

        //上线时间按照服务器时间
        Date datetime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Updatetime = sdf.format(datetime);

        _logger.debug("==> confirmUp ==> Paramers:functionID:{}, opid:{}, id:{}, username:{}, bb:{}, mo:{}, scxm:{}, datetime:{}, sec:{}",
                    functionID,opid,id,username,bb,mo,scxm,Updatetime,sec);
        return perService.confirmUp(functionID, opid, id, username, bb, mo, scxm, Updatetime, sec);
    }

    @Operation(summary = "员工确认下线")
    @GetMapping("/confirmDown")
    public TDGResponse confirmDown(String sjcl,String functionID,String opid, String id,String mo,String sec,String scxm) {
        if (StringUtils.isBlank(functionID)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作模块不可为空");
        }
        if (StringUtils.isBlank(opid)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作号不可为空");
        }
        if (StringUtils.isBlank(id)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工号不可为空");
        }
        if (StringUtils.isBlank(mo)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }
        if (StringUtils.isBlank(scxm)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"生产线码不可为空");
        }
        if (StringUtils.isBlank(sec)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工艺段不可为空");
        }

        //上线时间按照服务器时间
        Date datetime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String downDatetime = sdf.format(datetime);

        _logger.debug("==> confirmDown ==> Paramers:functionID:{}, opid:{},id:{}, datetime:{}, mo:{}, sec:{}, scxm:{}, sjcl:{}",
                    functionID,opid, id,downDatetime,mo,sec,scxm,sjcl);
        return perService.confirmDown(sjcl ,functionID, opid, id, downDatetime, mo, sec, scxm);
    }

    @Operation(summary = "勾选下线")
    @GetMapping("/downByCheck")
    public TDGResponse downByCheck(String sjcl,String functionID,String opid, String gdh,String ryarr,String scxm,String sec) {

        if (StringUtils.isBlank(functionID)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作模块不可为空");
        }
        if (StringUtils.isBlank(opid)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作号不可为空");
        }
        if (StringUtils.isBlank(ryarr)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工号不可为空");
        }
        if (StringUtils.isBlank(gdh)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }
        if (StringUtils.isBlank(scxm)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"生产线码不可为空");
        }
        if (StringUtils.isBlank(sec)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工艺段不可为空");
        }
        //上线时间按照服务器时间
        Date datetime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String downDatetime = sdf.format(datetime);

        _logger.debug("==> downByCheck ==> Paramers:functionID:{}, opid:{} ,gdh:{}, ryarr:{}, datetime:{}, scxm:{}, sec:{}, sjcl:{}",
                    functionID,opid,gdh,ryarr,downDatetime,scxm,sec,sjcl);
        String[] ryarrs = ryarr.split(",");
        return  perService.downByCheck(sjcl,functionID, opid, gdh, ryarrs, downDatetime, scxm, sec);
    }

    @Operation(summary = "整工单下线")
    @GetMapping("/downByGD")
    public TDGResponse downByGD(String sjcl,String functionID,String opid, String gdh,String sec,String scxm,String bb) {

        if (StringUtils.isBlank(functionID)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作模块不可为空");
        }
        if (StringUtils.isBlank(opid)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作号不可为空");
        }
        if (StringUtils.isBlank(gdh)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }
        if (StringUtils.isBlank(scxm)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"生产线码不可为空");
        }
        if (StringUtils.isBlank(sec)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工艺段不可为空");
        }
        if (StringUtils.isBlank(bb)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"班别不可为空");
        }

        //下线时间按照服务器时间
        Date datetime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String downDatetime = sdf.format(datetime);

        _logger.debug("==> downByGD ==> Paramers:functionID:{}, opid:{} ,gdh:{}, datetime:{}, opid:{}, sec:{}, scxm:{}, bb:{},sjcl:{}",
                    functionID,opid, gdh,downDatetime,opid,sec,scxm,bb,sjcl);
        return perService.downByGD(sjcl,functionID, opid, gdh, downDatetime, sec, scxm, bb);
    }

    @Operation(summary = "切换工单")
    @GetMapping("/qhgd")
    public TDGResponse qhgd(String sjcl,String functionID,String opid, String gdh,String gdh2,String scxm,String scxm2,String sec,String sec2,String bb,String ryarr) {

        if (StringUtils.isBlank(functionID)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作模块不可为空");
        }
        if (StringUtils.isBlank(opid)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"操作号不可为空");
        }
        if (StringUtils.isBlank(gdh)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"源工单号不可为空");
        }
        if (StringUtils.isBlank(gdh2)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"目标工单号不可为空");
        }
        if (StringUtils.isBlank(scxm)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"源生产线码不可为空");
        }
        if (StringUtils.isBlank(scxm2)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"目标生产线码不可为空");
        }
        if (StringUtils.isBlank(sec)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"源工艺段不可为空");
        }
        if (StringUtils.isBlank(sec2)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"目标工艺段不可为空");
        }
        if (StringUtils.isBlank(bb)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"班别不可为空");
        }
        if (StringUtils.isBlank(ryarr)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工号不可为空");
        }

        //下线时间按照服务器时间
        Date datetime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String downDatetime = sdf.format(datetime);

        _logger.debug("==> qhgd ==> Paramers:functionID:{}, opid:{} ,gdh:{}, gdh2:{}, scxm:{}, scxm2:{}, datetime:{}, opid:{}, sec:{}, sec2:{}, bb:{}, ryarr:{}, sjcl:{}",
                functionID,opid,gdh,gdh2,scxm,scxm2,downDatetime,opid,sec,sec2,bb,ryarr,sjcl);
        String[] ryarrs = ryarr.split(",");
        return perService.qhgd(sjcl,functionID,opid,gdh, gdh2, scxm, scxm2, downDatetime, sec, sec2, bb, ryarrs);
    }

    @Operation(summary = "下线前判断实际产量")
    @GetMapping("/pdsjcl")
    public TDGResponse pdsjcl(String ryarr,String gdh,String scxm,String sec){
        if (StringUtils.isBlank(ryarr)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工号不可为空");
        }

        if (StringUtils.isBlank(gdh)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工单号不可为空");
        }
        if (StringUtils.isBlank(scxm)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"生产线码不可为空");
        }
        if (StringUtils.isBlank(sec)){
            return TDGResponse.fail(TDGResponseCode.INVALID_PARAMETERS,"工艺段不可为空");
        }
        String[] ryarrs = ryarr.split(",");
        _logger.debug("==> pdsjcl ==> Paramers:ryarr:{}, gdh:{}, scxm:{}, sec:{}",ryarr, gdh,scxm,sec);
        return perService.pdsjcl(ryarrs,gdh,sec,scxm);
    }

}
